package org.simple.simple.contorller;


import org.simple.simple.vo.LoginBean;
import org.simple.simple.vo.ResultVO;
import org.simple.simple.vo.UploadBean;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@RestController
public class ControllerTest {


    @GetMapping("/info")
    public ResultVO<String> doGet(String id, String code) {
        System.out.println("id:" + id + "\ncode:" + code);
        ResultVO<String> resultVO = new ResultVO<String>();
        resultVO.setCode(200);
        resultVO.setMessage("Get请求成功");
        resultVO.setBody("id=" + id + "---" + "code=" + code);
        return resultVO;
    }

    @PostMapping("/login")
    public ResultVO<String> doPost(@RequestHeader("session") String session,
            @RequestHeader("deviceId")String deviceId,
            @RequestParam("userName") String userName, @RequestParam("password") String password) throws IOException {
        System.out.println("session:"+session);
        System.out.println("deviceId:"+deviceId);
        System.out.println("userName:" + userName + "\npassword:" + password);
        ResultVO<String> resultVO = new ResultVO<String>();
        resultVO.setCode(200);
        if (null == userName) {
            throw new IOException("123");
        }
        resultVO.setMessage("PostParas请求成功");
        resultVO.setBody("userName=" + userName + "---" + "password=" + password);
        return resultVO;
    }

    @PostMapping("/login1")
    public ResultVO<LoginBean> doPost1(@RequestBody LoginBean loginBean) {
        System.out.println("userName:" + loginBean.getUserName());
        System.out.println("password:" + loginBean.getPassword());
        ResultVO<LoginBean> resultVO = new ResultVO<LoginBean>();
        resultVO.setCode(200);
        resultVO.setMessage("PostJson请求成功");
        resultVO.setBody(loginBean);
        return resultVO;
    }


    @PostMapping("/uploadFile")
    public ResultVO<String> uploadFile(@RequestParam("file") MultipartFile file, Model model, HttpServletRequest request) {
        if (file == null) {
            System.out.println("文件为空");
            ResultVO resultVO = new ResultVO();
            resultVO.setCode(500);
            resultVO.setMessage("上传文件为空");
            return resultVO;
        }
        String fileName = file.getOriginalFilename();
        String suffixName = fileName.substring(fileName.lastIndexOf("."));
        String filePath = "/Users/qms/upload/";
        fileName = UUID.randomUUID() + suffixName;
        File dest = new File(filePath + fileName);
        if (!dest.getParentFile().exists()) {
            dest.getParentFile().mkdirs();
        }
        try {
            file.transferTo(dest);
        } catch (IOException e) {
            e.printStackTrace();
        }
        String fullFileName = filePath + fileName;
        model.addAttribute("fullFileName" + fullFileName);
        ResultVO resultVO = new ResultVO();
        resultVO.setCode(200);
        resultVO.setMessage("上传成功");
        resultVO.setBody("成功：" + fullFileName);
        return resultVO;
    }

    @PostMapping("/uploadFiles")
    public ResultVO<List<String>> uploadFile(@RequestParam("files") MultipartFile[] files, Model model, HttpServletRequest request) {
        if (files == null) {
            System.out.println("多文件为空");
            ResultVO resultVO = new ResultVO();
            resultVO.setCode(500);
            resultVO.setMessage("上传的文件为空");
            return resultVO;
        }
        List<String> fullNames = new ArrayList<>(files.length);
        for (MultipartFile file : files) {
            String fileName = file.getOriginalFilename();
            String suffixName = fileName.substring(fileName.lastIndexOf("."));
            String filePath = "/Users/qms/upload/";
            fileName = UUID.randomUUID() + suffixName;
            File dest = new File(filePath + fileName);
            if (!dest.getParentFile().exists()) {
                dest.getParentFile().mkdirs();
            }
            try {
                file.transferTo(dest);
            } catch (IOException e) {
                e.printStackTrace();
            }
            String fullFileName = filePath + fileName;
            model.addAttribute("fullFileName:" + fullFileName);
            fullNames.add(fullFileName);
        }
        ResultVO resultVO = new ResultVO();
        resultVO.setCode(200);
        resultVO.setMessage("上传成功");
        resultVO.setBody(fullNames);
        return resultVO;
    }

    @PostMapping("/uploadAndLogin")
    public ResultVO<String> uploadAndLogin(UploadBean bean){
        System.out.println("userName:" + bean.getUserName());
        System.out.println("password:" + bean.getPassword());
        String fileName = bean.getFile().getOriginalFilename();
        String suffixName = fileName.substring(fileName.lastIndexOf("."));
        String filePath = "/Users/qms/upload/";
        fileName = UUID.randomUUID() + suffixName;
        File dest = new File(filePath + fileName);
        if (!dest.getParentFile().exists()) {
            dest.getParentFile().mkdirs();
        }
        try {
            bean.getFile().transferTo(dest);
        } catch (IOException e) {
            e.printStackTrace();
        }
        String fullFileName = filePath + fileName;
        ResultVO resultVO = new ResultVO();
        resultVO.setCode(200);
        resultVO.setMessage("上传成功");
        resultVO.setBody("成功："+fullFileName);
        return resultVO;
    }

}
